import {
    Method
} from 'axios';

import md5 from 'md5';

import {
    SignRes
} from './../types';

import * as lag from './lag';

import {
    checkPost,
    parseQueryStr
} from './../utils';

const _timeCode = async (): Promise<number> => {
    const _time = await lag.getTime();
    return ~~((Date.now() - _time) / 1000)
}

//签名
export const query = async (method?: Method, data?: any) => {
    const encrypt: SignRes = {};
    const timeCode = await _timeCode();
    // 如果是post请求
    if (checkPost(method)) {
        let paraStr = JSON.stringify(data);
        paraStr += timeCode;
        paraStr = md5(paraStr).toUpperCase();
        encrypt.t = timeCode;
        encrypt.s = paraStr;
    }
    else {
        const parseStr = parseQueryStr(data, false);
        const encodeStr = parseStr + timeCode;
        encrypt.t = timeCode;
        encrypt.s = md5(encodeStr).toUpperCase();
    }
    return parseQueryStr(encrypt,false);
}




